/*
 * @页面名称:批量操作数据文件
 * @描述:
 * @作者: 研发中心
 * @Date: 2023-07-19 08:57:47
 */
import vueAxios from "@/api/index";
import { useDynamicStore } from "@/stores/modules/dynamic";
import { cloneDeep, isEqual, has } from "lodash-es";
import RuleClass from "./FieldRule";
import { removeBlock } from "./until";
import { watch } from "vue";
class CommonRule {
  list: any[]; //数据列表
  currentTreeNode: any; //当前选择的树节点
  ruleList: any[]; //规则列表
  tableName: string; //表名
  dynamicStore: any;
  type: any;
  constructor({
    list,
    currentTreeNode,
    ruleList,
    tableName,
    type
  }: {
    list: any[];
    currentTreeNode: any;
    ruleList: any[];
    tableName: string;
    type: any;
  }) {
    this.list = list;
    this.currentTreeNode = currentTreeNode;
    this.ruleList = ruleList;
    this.tableName = tableName;
    this.type = type;
    this.dynamicStore = useDynamicStore();
  }
  async validation() {
    for (let i = 0; i < this.list.length; i++) {
      const item = this.list[i];
      // 删除checkbox，不然接口报错，不是来自于数据库，表格自带
      delete item.checkbox;
      this.dynamicStore.oldModalFormData = cloneDeep(item);
      item["Catalog_ID"] = this.currentTreeNode["Catalog_ID"];
      const FieldRuleClass = new RuleClass({
        modalFormData: item,
        ruleList: this.ruleList,
        tableName: this.tableName
      }); // 执行字段规则类方法
      await FieldRuleClass.fieldRuleEvent2();
      const param = { list: [{ ...item }], tableName: this.tableName };
      const Res = await vueAxios.put("Dynamic", param, { noLoading: true });
      if (Res.Mark) {
        if (this.type === "folder") {
          // 删除当前目录下的数据
          const index = this.dynamicStore.tableData.findIndex((v: any) => v.Record_ID === item.Record_ID);
          if (index > -1) {
            this.dynamicStore.tableData.splice(index, 1);
          }
        } else if (this.type === "child") {
          // 删除当前目录下的数据
          const index = this.dynamicStore.childTableData.findIndex((v: any) => v.Record_ID === item.Record_ID);
          if (index > -1) {
            this.dynamicStore.childTableData.splice(index, 1);
          }
        }
      }
    }
  }
}

export default CommonRule;
